//
//  AddressCell.m
//  MyNewProject
//
//  Created by 孙 on 2019/2/3.
//  Copyright © 2019 sun. All rights reserved.
//

#import "AddressCell.h"
#import "XM_AuctionProductModel.h"
@interface AddressCell()
@property(nonatomic,strong) UILabel * accressPersionLab;
@property(nonatomic,strong) UILabel* addressLab;
@property(nonatomic,strong) UIImageView* lineImageView;

@property(nonatomic,strong) UIButton* detelateBtn;
@property(nonatomic,strong) UIButton* editingBtn;

@end
@implementation AddressCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self =[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

        self.contentView.backgroundColor = [UIColor whiteColor];

        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initView];

    }
    return self;


}
-(void)initView
{
    [self.contentView addSubview: self.accressPersionLab];

    [self.contentView addSubview:self.addressLab];

    [self.contentView addSubview:self.lineImageView];

    [self.contentView addSubview:self.selectBtn];

    [self.contentView addSubview:self.detelateBtn];

    [self.contentView addSubview:self.editingBtn];


}
-(UILabel*)accressPersionLab
{
    if (_accressPersionLab == nil) {
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, SCREEN_WIDTH - 30 , 0)];
        label.numberOfLines = 0 ;
        label.textAlignment = NSTextAlignmentLeft;


        label.font = SKFONT_Semibold(15);

        label.textColor = SKColorWithHex(0x444444);

        _accressPersionLab = label;

    }
    return _accressPersionLab;

}
-(UILabel*)addressLab
{
    if (_addressLab == nil) {
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(15, self.accressPersionLab.bottom, SCREEN_WIDTH - 30 , 0)];
        label.numberOfLines = 0 ;
        label.textAlignment = NSTextAlignmentLeft;


        label.font = SKFONT_Semibold(14);

        label.textColor = SKColorWithHex(0xBCBCBC);

        _addressLab = label;

    }
    return _addressLab;

}
-(UIImageView *)lineImageView
{
    if (_lineImageView == nil) {
        _lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.addressLab.bottom, SCREEN_WIDTH, .5)];
        _lineImageView.backgroundColor =SKRGBColor(239, 239, 239);


    }
    return _lineImageView;
}
-(UIButton*)selectBtn
{
    if (_selectBtn ==nil) {
        NSString * totalAmtStr = @"设置为默认";
        UIFont * font = [UIFont systemFontOfSize:13];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

        CGFloat strWidth = [NSString getWidthWithString:totalAmtStr font:font lineBreakModel:NSLineBreakByCharWrapping];

        button.frame = CGRectMake(10, self.lineImageView.bottom, strWidth + 18 +10, 50);

        button.imageView.contentMode = UIViewContentModeScaleAspectFit;

        button.backgroundColor = [UIColor clearColor];


        [button setTitle:totalAmtStr forState:UIControlStateNormal];

        button.titleLabel.font = font ;

        [button setTitleColor:SKColorWithHex(0x999999) forState:UIControlStateNormal];
//SKIMAGE_NAMED(@"unSelectedItem")
        
        [button setImagePosition:LXMImagePositionLeft spacing:10];

        [button addTarget:self action:@selector(defaultSetup:) forControlEvents:UIControlEventTouchUpInside];

        _selectBtn = button;
    }

    return _selectBtn;

}
-(void)defaultSetup:(UIButton*)sender
{
    if (self.myDefaultSetup) {

        self.myDefaultSetup(@"", self);
    }

}
-(UIButton*)detelateBtn
{
    if (_detelateBtn ==nil) {
        NSString * totalAmtStr = @"删除";
        UIFont * font = [UIFont systemFontOfSize:13];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

        CGFloat strWidth = [NSString getWidthWithString:totalAmtStr font:font lineBreakModel:NSLineBreakByCharWrapping];
        
        button.frame = CGRectMake(SCREEN_WIDTH - (strWidth + 15 +5 )*2 -30, self.lineImageView.bottom, strWidth + 15 +5, 50);

        button.imageView.contentMode = UIViewContentModeScaleAspectFit;

        button.backgroundColor = [UIColor clearColor];


        [button setTitle:totalAmtStr forState:UIControlStateNormal];

        button.titleLabel.font = font ;

        [button setTitleColor:SKColorWithHex(0x999999) forState:UIControlStateNormal];

        [button setImage:SKIMAGE_NAMED(@"addressDelete") forState:UIControlStateNormal];
        [button setImagePosition:LXMImagePositionLeft spacing:5];

        [button addTarget:self action:@selector(deleteBtn:) forControlEvents:UIControlEventTouchUpInside];

        _detelateBtn = button;
    }

    return _detelateBtn;


}
-(void)deleteBtn:(UIButton*)sender
{
    if (self.myDeleteEvent) {
        self.myDeleteEvent(@"", self);
    }
}
-(UIButton*)editingBtn
{
    if (_editingBtn ==nil) {
        NSString * totalAmtStr = @"编辑";
        UIFont * font = [UIFont systemFontOfSize:13];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

        CGFloat strWidth = [NSString getWidthWithString:totalAmtStr font:font lineBreakModel:NSLineBreakByCharWrapping];

        button.frame = CGRectMake(SCREEN_WIDTH - (strWidth + 15 +5 ) -15, self.lineImageView.bottom, strWidth + 15 +5, 50);

        button.imageView.contentMode = UIViewContentModeScaleAspectFit;

        button.backgroundColor = [UIColor clearColor];


        [button setTitle:totalAmtStr forState:UIControlStateNormal];

        button.titleLabel.font = font ;

        [button setTitleColor:SKColorWithHex(0x999999) forState:UIControlStateNormal];

        [button setImage:SKIMAGE_NAMED(@"adressEditing") forState:UIControlStateNormal];
        [button setImagePosition:LXMImagePositionLeft spacing:5];

        [button addTarget:self action:@selector(edtingBtn:) forControlEvents:UIControlEventTouchUpInside];

        _editingBtn = button;
    }

    return _editingBtn;


}
-(void)edtingBtn:(UIButton*)sender
{

    if (self.myEditing) {
        self.myEditing(@"", self);
    }
}

-(void)setModel:(id)model
{
    XM_MyAddressModel * pModel  = (XM_MyAddressModel *)model;
    self.accressPersionLab.text = pModel.name;
    self.accressPersionLab.height = [NSString getHeightWithString:self.accressPersionLab.text font:self.accressPersionLab.font lineBreakModel:NSLineBreakByCharWrapping limitWidth:SCREEN_WIDTH - 30];
    
    self.addressLab.top = self.accressPersionLab.bottom;
    
    self.addressLab.text=[NSString stringWithFormat:@"%@ %@",pModel.city,pModel.addr];
    self.addressLab.height =[NSString getHeightWithString:self.addressLab.text font:self.addressLab.font lineBreakModel:NSLineBreakByCharWrapping limitWidth:SCREEN_WIDTH - 30];
    
    
    self.lineImageView.top = self.addressLab.bottom +15;
    
    self.selectBtn.top = self.lineImageView.bottom;
    self.detelateBtn.top = self.lineImageView.bottom;
    self.editingBtn.top = self.lineImageView.bottom;
}

+ (CGFloat)heightOfRowWithModel:(id)model
{
    XM_MyAddressModel * pModel  = (XM_MyAddressModel *)model;
    NSString * persionStr = pModel.name;
    CGFloat persionHeight =[NSString getHeightWithString:persionStr font:SKFONT_Semibold(15) lineBreakModel:NSLineBreakByCharWrapping limitWidth:SCREEN_WIDTH - 30];
    NSString * addressStr = [NSString stringWithFormat:@"%@ %@",pModel.city,pModel.addr];
    CGFloat addressHeight = [NSString getHeightWithString:addressStr font:SKFONT_Semibold(14) lineBreakModel:NSLineBreakByCharWrapping limitWidth:SCREEN_WIDTH - 30];
    
    return persionHeight + addressHeight + 15 *2 + 50;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
